package com.web.controller;

import cn.hutool.jwt.JWTUtil;
import com.basemodel.Result;
import com.util.CheckIsMobileUtil;
import com.web.model.LoginUser;
import com.web.model.Medical;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

/**
 * @author cxt
 * @date 2021/6/16
 */
@RestController
@RequestMapping("/api/yibao")
@Slf4j
public class MedicalController {

    public static final String SALT = "TYPESCRIPT";

    /**
     * 其他系统调用验证
     *
     * @param medical 参数
     */
    @PostMapping("/test")
    public Object check(@RequestBody Medical medical) {
        // 做一些校验操作
        HashMap<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("message", "success");
        map.put("data", 0.9);
        return map;
    }

    /**
     * 登录验证
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/login")
    public Object login(@RequestBody LoginUser user, HttpServletRequest request) {
        boolean b = CheckIsMobileUtil.checkAgentIsMobile(request);
        System.out.println("请求是否手机：" + b);
        String token = "";
        if (StringUtils.isNotEmpty(user.getName()) && StringUtils.isNotEmpty(user.getIdcardNo())) {
            Map<String, Object> map = new HashMap<>();
            map.put("userName", user.getName());
            map.put("idcardNo", user.getIdcardNo());
            long milliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
            map.put("createTime", milliSecond);
            token = JWTUtil.createToken(map, SALT.getBytes());
            log.debug("【{}】login success!", user.getName());
            return Result.success("成功", token);
        }
        return Result.warn("失败");
    }

    @Test
    public void test() {
        double v = 899 / 100.00;
        BigDecimal total = BigDecimal.valueOf(v);
        BigDecimal proportion = new BigDecimal("0.95");

        // 医保金额
        BigDecimal medicaidAmount = proportion.multiply(total).setScale(2, RoundingMode.HALF_EVEN);
        // 自费金额
        BigDecimal payAmount = BigDecimal.valueOf(1).subtract(proportion).multiply(total).setScale(2, RoundingMode.HALF_EVEN);
        System.out.println(medicaidAmount);
        System.out.println(payAmount);
    }

    @GetMapping("/get")
    public Object getList(@RequestParam Integer id) {
        log.info("接收到测试请求：{}", id);
        return Result.success("请求成功：" + id);
    }

    @Test
    public void dd() {
        HashSet<String> set = new HashSet<>();
        for (int i = 0; i < 50; i++) {
            String cre = cre();
            set.add(cre);
        }
        System.out.println(set.size());
    }

    public static String cre() {


        final String i = RandomStringUtils.randomNumeric(2);
        String format = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"));
        return format + i;
    }
}
